package com.ebt.m.customer;

import android.content.Context;
import android.text.TextUtils;
import android.widget.ImageView;

import com.ebt.m.R;
import com.ebt.m.customer.constant.CustomerConstant;
import com.ebt.m.customer.util.AttachmentUtil;
import com.ebt.m.data.bean.EbtFile;
import com.ebt.m.data.entity.Customer;
import com.ebt.m.data.entity.EntityCustomerAttachment;
import com.ebt.m.repository.provider.FileIconHelper;
import com.ebt.m.repository.provider.FileTypeHelper;
import com.ebt.m.utils.imageloader.ImageLoader;
import com.ebt.m.utils.imageloader.ImageLoaderUtil;

import java.io.File;

/**
 * Created by Rick.Wang on 2017/11/2.
 */

public class CustomerUtil {

    public static Customer getADefaultCustomerForProposal() {
        Customer customer = new Customer();
        customer.setName("");
        customer.setSex(1);
        customer.setAge(30);
        customer.setBirthday(null);
        return customer;
    }

    //选择客户时的来源
    public static final int FROM_CHOOSE = 100;
    public static final int FROM_OTHER = 200;

    /**
     * 根据性别设置默认头像
     *
     * @param context
     * @param imageView
     * @param url
     * @param sex
     */
    public static void setAvatarByUrlAndSex(Context context, ImageView imageView, String url, Integer sex) {
        if (TextUtils.isEmpty(url)) {
            if (sex == null) {
                imageView.setImageResource(R.drawable.ic_customer_default_avatar_man);
            } else if (sex == 0) {
                imageView.setImageResource(R.drawable.ic_customer_default_avatar_woman);
            } else {
                imageView.setImageResource(R.drawable.ic_customer_default_avatar_man);
            }
        } else {
            ImageLoaderUtil.getInstance().loadImage(context, new ImageLoader.Builder().url(url)
                    .placeHolder((R.drawable.ic_customer_default_avatar_man)).circle().thumb(0.3f).imgView(imageView).build());
        }
    }

    public static EbtFile convertAttachmentToEbtFile(EntityCustomerAttachment attachment) {
        EbtFile outEbtFile = new EbtFile();
        outEbtFile.name = attachment.fileName;
        if (TextUtils.isEmpty(attachment.fileType)) {
            attachment.fileType = String.valueOf(FileTypeHelper.FileTypeAll.FILE_TYPE_OTHER.getValue());
        }
        outEbtFile.type = FileTypeHelper.FileTypeAll.valueOf(Integer.valueOf(attachment.fileType));
        //如果是null，说明来自ios
        if (outEbtFile.type == null) {
            outEbtFile.type = FileIconHelper.getFileTypeBySuffix(outEbtFile.name);
        }

        outEbtFile.path = AttachmentUtil.netPathToLocalPath(attachment.path, CustomerConstant.Attachment.DIR_PATH + File.separator);
        if (!TextUtils.isEmpty(attachment.content)) {
            outEbtFile.size = Long.valueOf(attachment.content);
//            int index = attachment.content.indexOf("@");
//            try {
//                outEbtFile.packageName = attachment.content.substring(0, index);
//                outEbtFile.activityName = attachment.content.substring(index + 1);
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
        }
        return outEbtFile;
    }
}
